package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmVersion;
import com.zb.entity.dto.VersionDto;
import com.zb.entity.dto.VersionList;
import com.zb.entity.dto.VersionSelectDto;
import com.zb.mapper.ZjAdmVersionMapper;
import com.zb.service.ZjAdmModuleService;
import com.zb.service.ZjAdmVersionModuleService;
import com.zb.service.ZjAdmVersionService;
import com.zb.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 后台系统版本表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-12
 */
@Service
public class ZjAdmVersionServiceImpl extends ServiceImpl<ZjAdmVersionMapper, ZjAdmVersion> implements ZjAdmVersionService {
    @Autowired
    private ZjAdmVersionMapper admVersionMapper;
    @Autowired
    private ZjAdmVersionModuleService admVersionModuleService;

    // 新增或修改版本
    @Override
    @Transactional
    public void save(VersionDto versionDto) {
        Assert.isTrue(Objects.equals(1, versionDto.getType()) || Objects.equals(2, versionDto.getType()) || Objects.equals(3, versionDto.getType()),
                "单价类型不合法", versionDto.getType());
        Assert.isTrue(null == admVersionMapper.existByName(versionDto.getId(), versionDto.getName()),
                "版本名称已经存在，不可重复添加或更改！", versionDto.getName());

        ZjAdmVersion version = new ZjAdmVersion();
        version.setName(versionDto.getName());
        version.setCode(IdWorker.getTimeId());
        version.setOrderUnlimited(versionDto.getOrderUnlimited());
        version.setOrderLimitNum(versionDto.getOrderLimitNum());
        version.setUserQty(versionDto.getUserQty());
        version.setType(versionDto.getType());
        version.setPrice(versionDto.getPrice());
        if (null != versionDto.getId()) {
            version.setId(versionDto.getId());
            this.updateById(version);
            admVersionModuleService.save(version.getId(), versionDto.getModuleIds());
            return;
        }
        version.setId(IdWorker.getId());
        version.setDisabled(false);
        this.save(version);
        admVersionModuleService.save(version.getId(), versionDto.getModuleIds());
    }

    // 版本列表查询
    @Override
    public Map<String, Object> getList(VersionSelectDto selectDto) {
        QueryWrapper<ZjAdmVersion> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectDto.getName())) {
            wrapper.like("name", selectDto.getName());
        }
        if (null != selectDto.getType()) {
            wrapper.eq("type", selectDto.getType());
        }
        PageUtil.setPageLimit(selectDto);
        Page<ZjAdmVersion> page = this.page(new Page<>(selectDto.getPage(), selectDto.getLimit()), wrapper);

        List<VersionList> versionLists = new ArrayList<>();
        page.getRecords().forEach(t -> versionLists.add(VersionList.beanBoV(t, admVersionModuleService.getModuleByVersionId(t.getId()))));

        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), versionLists);
    }

    // 根据版本ID获取版本信息
    @Override
    public VersionDto getInfoById(Long id) {
        ZjAdmVersion version = this.getById(id);
        return VersionDto.beanToDto(version, admVersionModuleService.getModuleIdByVersionId(id));
    }

    // 批量禁用或启用版本
    @Override
    public void updateBatchDisableById(List<Long> idList, boolean disable) {
        admVersionMapper.updateBatchDisableById(idList, disable);
    }
}
